<?php
include_once('../init.inc.php');
    
    

if (isset($_POST['action'])) {

	//$action 		= mysql_real_escape_string($_POST['action']);
        $ID	= (int)$_POST['ID']; 
        $count = makeVote($ID);
        //the functions.php returns a response like "1|13|#ffcc00-2|32|#00ff00-3|18|#cc0000-63" 
        //which the first number is the answerID, second is the points it has and third is the color for that answer's graph.
        // The last number is the sum of all points for easilt calculating percentages.
        $allPoll = getAllPoll();
        
        echo $ID."|".$count."|".$allPoll;

}




function makeVote($ID){
     // обновляем результат голосования
    //1 проверим, голосовал ли ранее этот юзер за эту тему
    
  $update = true;
    
   //получим кол-во голосов 
      $sql = "SELECT pollID, pollAnswerPoints
        FROM `pollanswers`
        WHERE pollID=$ID LIMIT 1";
      
     $result = mysql_query($sql) or die('Error, insert query failed');
     $row = mysql_fetch_assoc($result); 
     if (!$row) $update = false;
     $pollAnswerPoints = $row['pollAnswerPoints'];
     if (!$pollAnswerPoints) $pollAnswerPoints = 0;
     
     //юзер  
       $usermail = mysql_real_escape_string($_SESSION['amembermail']);  
    
    if ($usermail){
        $sql = "SELECT *
        FROM `userpoll`
        WHERE usermail='$usermail' ORDER BY poll_date DESC LIMIT 1";
        /*
    $sql = "SELECT *
        FROM `userpoll`
        WHERE pollID=$ID AND usermail='$usermail' ORDER BY poll_date DESC LIMIT 1";
         
         */
    
    $result = mysql_query($sql) or die('Error, insert query failed');
    $row = mysql_fetch_assoc($result); 
    $timepoll = strtotime($row['poll_date']);
    $timenow = time();
    //сколько суток прошло с последнего голосования
    $diff = (int)(($timenow - $timepoll)/(60*60*24));
    
   // $tmp = date('Y-m-d H:i:s', $timepoll);
   // $tmp2 = date('Y-m-d H:i:s', time());
    }else exit;
    
    if ($row>0 && $diff<30){
        //уже голосовал
          return $pollAnswerPoints;
    }
     
    //не голосовал, добавляем в таблицу голосовавших

    $sql = "INSERT INTO `userpoll` SET usermail = '$usermail', pollID = '$ID', poll_date=NOW() ";
   
      
     $result = mysql_query($sql) or die('Error, insert query failed');
    
     
     
     
     $pollAnswerPoints++;
    if ($update){
     $sql = "UPDATE `pollanswers` SET pollAnswerPoints = $pollAnswerPoints 
        WHERE pollID=$ID ";
      }else{
        $sql = "INSERT INTO `pollanswers` SET pollAnswerPoints = 1, pollID = '$ID' ";
        
    }
      
     $result = mysql_query($sql) or die('Error, insert query failed');
   //  $row = mysql_fetch_assoc($result);
     
     return $pollAnswerPoints;
     
 }
function getAllPoll()
{
  $query  = "SELECT sum(pollAnswerPoints) as sum FROM pollanswers";
	$result = mysql_query($query);
        
         

	$row = mysql_fetch_array($result, MYSQL_ASSOC);
 
    
    return $row['sum'];
    
    
}








?>